﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Windows;
using System.Windows.Controls;
using Tools;

namespace WPF数据采集系统.View
{
    /// <summary>
    /// UserPowerChange.xaml 的交互逻辑
    /// </summary>
    public partial class UserPowerChange : Window
    {
        public UserPowerChange()
        {
            InitializeComponent();
        }
        public delegate void SendMessage(string userName, string role,string MainViewName);

        public SendMessage sendMessage;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ComboBox cmb = user_name as ComboBox;
                TextBox tb = cmb.Template.FindName("mes_url", cmb) as TextBox;
                string uname = tb.Text.Trim();
                string pw = DESEncrypt.Encrypt(psd.Password.Trim());
                string sql = "select role from Users where user_name='" + uname + "' and password='" + pw + "' and is_valid='1'";
                var Role = SqlHelper.GetSingleResult(sql);
                if (Role == null)
                {
                    CustomMsgBox.ShowDialog("用户登录(切换)失败", "用户名或密码不正确！！请检查!", CustomMsgBoxButton.One, CustomMsgBoxImage.Error);
                    return;
                }
                sendMessage(uname, Role.ToString(), "");
                this.Close();
            }
            catch(Exception ex)
            {
                CustomMsgBox.ShowDialog(ex.Message);
            }

        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                List<string> list = new List<string>();
                string sql = "select user_name from Users";
                SqlDataReader result = SqlHelper.GetReader(sql);
                if (result.HasRows)
                {
                    while (result.Read())
                    {
                        list.Add(result["user_name"].ToString());
                    }
                }
                user_name.ItemsSource = list;
            }
            catch (Exception ex)
            {
                CustomMsgBox.ShowDialog(ex.Message);
            }
        }
        private void SFRName_TextChanged(object sender, RoutedEventArgs e)
        {
            user_name.IsDropDownOpen = true;
        }
    }
}
